package com.jnshu.tools;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author Martin
 * @date 2019/8/8 18:50
 */
public class TimeUtil {

    private static final int SECONDS_IN_DAY = 60*60*24;
    private static final long MILLISECOND_IN_DAY =1000L*SECONDS_IN_DAY;

    //判断今天签到状态
    public static int isSameDayOfMillis(final long ms1,final long ms2){
        final long interval = ms1 - ms2;
        if (interval < MILLISECOND_IN_DAY &&
            interval > -1L * MILLISECOND_IN_DAY &&
            toDay(ms1) == toDay(ms2)){
            return 1;
        }
        return 0;
    }
    private static long toDay(long millis){
        return (millis + TimeZone.getDefault().getOffset(millis)) / MILLISECOND_IN_DAY;
    }

    //判断2个日期是否为连续的
    public static boolean isContinuous(long time){
        //今天0点的毫秒数
        long zeroTime= LocalDateTime.of(LocalDate.now(), LocalTime.MIN).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
        System.out.println("今天0点的毫秒数"+zeroTime);
        long differenceValue=zeroTime-time;
        return differenceValue <= MILLISECOND_IN_DAY;
    }

    //获取当前时间到第二天0点的秒数
    public static int subtract(){
        long now=System.currentTimeMillis();
        long zeroTime= LocalDateTime.of(LocalDate.now(), LocalTime.MIN).toInstant(ZoneOffset.ofHours(8)).toEpochMilli();
        long tomorrowZero=zeroTime+24*60*60*1000;
        return (int) ((tomorrowZero-now)/1000);
    }

    public static void main(String[] args) {
        String lastTime="2019-08-28 13:59:59";
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date=simpleDateFormat.parse(lastTime);
            System.out.println("签到日期："+date.getTime());
            boolean result=TimeUtil.isContinuous(date.getTime());
            System.out.println("是否为连续签到"+result);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println("距离第二天的0点秒数值为："+TimeUtil.subtract());
    }
}
